/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.io.IOException;
import java.util.logging.Level;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * The Class ServletFilter.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public class ServletFilter implements Filter {

	/** The filter config. */
	private FilterConfig filterConfig;

	/**
	 *
	 * @param request
	 * @param response
	 * @param chain
	 *
	 * @throws IOException
	 * @throws ServletException
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		// HttpServletResponse httpServletResponse = (HttpServletResponse)
		// response;

		final ServletContext context = this.filterConfig.getServletContext();

		String projectName = context.getServletContextName();

		if ((projectName == null) || projectName.isEmpty()) {
			projectName = httpServletRequest.getContextPath();
			projectName = projectName.replace("/", "");
		}

		final String servlet = httpServletRequest.getRequestURI();

		String principal = null;

		try {
			principal = httpServletRequest.getRemoteUser();
		} catch (final Exception e) {
			JeeObserverContext.logger.log(Level.FINE, "Unable to get caller principal.");
		}

		final ActionObserver observer = new ActionObserver(ActionObserver.CATEGORY_SERVLET, projectName, servlet, principal);
		observer.start();

		// run the servlet
		try {
			chain.doFilter(request, response);
		} catch (final ServletException exception) {

			observer.throwException(exception);

			throw exception;
		} finally {
			observer.stop();
		}
	}

	/**
	 *
	 * @param filterConfigParam
	 *
	 * @throws ServletException
	 */
	@Override
	public void init(FilterConfig filterConfigParam) throws ServletException {
		this.filterConfig = filterConfigParam;
	}

	/**
	 *
	 */
	@Override
	public void destroy() {
		// destroy method
	}
}
